package ass.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ass.beans.UserPrivacyBean;
import ass.dao.UserPrivacyDAO;
import ass.dao.support.UserPrivacyDAOImpl;

/**
 * Servlet implementation class EditPrivacy
 */
public class EditPrivacy extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditPrivacy() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute("username");
		boolean insert = false;
		
		UserPrivacyDAO userPrivacyDAO= new UserPrivacyDAOImpl();
		UserPrivacyBean userPrivacy = userPrivacyDAO.getUser(username);
		
		String checkbox2 = (String) request.getParameter("checkbox2");
		String checkbox3 = (String) request.getParameter("checkbox3");
		String checkbox4 = (String) request.getParameter("checkbox4");
		String checkbox5 = (String) request.getParameter("checkbox5");
		String checkbox6 = (String) request.getParameter("checkbox6");
		String checkbox7 = (String) request.getParameter("checkbox7");
		
		
		if (userPrivacy == null){
			userPrivacy = new UserPrivacyBean();
			insert = true;
			
		}
		userPrivacy.setUsername(username);
		
		if (checkbox2 != null)	
			userPrivacy.setFirstNameShared(true);
		else
			userPrivacy.setFirstNameShared(false);
		
		if (checkbox3 != null)
			userPrivacy.setLastNameShared(true);
		else
			userPrivacy.setLastNameShared(false);
		
		if (checkbox5 != null)
			userPrivacy.setEmailShared(true);
		else
			userPrivacy.setEmailShared(false);
		
		if (checkbox4 != null)
			userPrivacy.setYearOfBirthShared(true);
		else
			userPrivacy.setYearOfBirthShared(false);
		
		if (checkbox6 != null)
			userPrivacy.setFavActorShared(true);
		else
			userPrivacy.setFavActorShared(false);
		
		if (checkbox7 != null)
			userPrivacy.setFavGenreShared(true);
		else
			userPrivacy.setFavGenreShared(false);
		
		//response.setContentType("text/html");// from response, set content type
		//PrintWriter out = response.getWriter();// from response, get output writer
		
		if(insert){
			userPrivacyDAO.insert(userPrivacy);
			//out.print("successfully inserted");
		}
		else{
			userPrivacyDAO.update(username, userPrivacy);
			//out.print("successfully updated");
		}

		
		request.setAttribute("updated", "<font color='green'> Privacy updated </font>");
		String nextJSP = "";
		nextJSP = "/userPrivacy.jsp";
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
		
		//out.close();
	}

}
